//
//  ViewController.swift
//  QHBinimageMan
//
//  Created by Anakin chen on 2021/3/15.
//

import Cocoa
import CoreGraphics

class ViewController: NSViewController {

    @IBOutlet weak var oimgIV: NSImageView!
    @IBOutlet weak var grayimgIV: NSImageView!
    @IBOutlet weak var binimgTV: NSTextView!
    @IBOutlet weak var histogramV: QHHistogramView!
    var histogramV2: QHHistogramView?
    
    @IBOutlet weak var minTF: NSTextField!
    @IBOutlet weak var maxTF: NSTextField!
    
    @IBOutlet weak var rationS: NSSlider!
    @IBOutlet weak var fontS: NSSlider!
    
    var pb: CVPixelBuffer?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    private func makefont() {
        self.binimgTV.setFont(NSFont.systemFont(ofSize: CGFloat(self.fontS.floatValue)), range: NSMakeRange(0, self.binimgTV.string.count))
    }

    @IBAction func openFileAction(_ sender: Any) {
        let myFiledialog = NSOpenPanel()
        myFiledialog.prompt = "Select flv path"
        myFiledialog.worksWhenModal = true
        myFiledialog.allowsMultipleSelection = false
        myFiledialog.canChooseDirectories = false
        myFiledialog.canChooseFiles = true
        myFiledialog.resolvesAliases = true
        myFiledialog.beginSheetModal(for: self.view.window!, completionHandler: { num in
            if num == NSApplication.ModalResponse.OK {
                myFiledialog.close()
                let url = myFiledialog.url!
                let pathExtension = url.pathExtension.uppercased()
                if pathExtension == "PNG" || pathExtension == "JPG" || pathExtension == "JPEG" {
                    
                    guard let img = NSImage(contentsOf: url) else { return }
                    
                    let img_r = QHUtil.resizeImage2(s_img: img, size: self.oimgIV.frame.size)
                    self.oimgIV.image = img_r
                    
                    guard let pb = QHBinimageMan.img2pb(image: img) else {
                        return
                    }
                    QHBinimageMan.img2gray(pb: pb)
                    let grayimg = QHBinimageMan.pb2img(pb: pb)
                    
                    let grayimg_r = QHUtil.resizeImage2(s_img: grayimg, size: self.grayimgIV.frame.size)
                    self.grayimgIV.image = grayimg_r
                    
                    
//                    self.histogramV.reset(pb: pb)
//                    self.histogramV.frame = self.oimgIV.bounds
//                    self.histogramV.draw(self.histogramV.frame)
                    self.histogramV2?.removeFromSuperview()
                    let histogramV_tmp = QHHistogramView()
                    histogramV_tmp.reset(pb: pb)
                    histogramV_tmp.frame = self.histogramV.bounds
                    self.histogramV.addSubview(histogramV_tmp)
                    histogramV_tmp.addBottom(histogramV_tmp.frame)
                    self.histogramV2 = histogramV_tmp
                    
                    self.pb = pb
                }
            }
            else {
                myFiledialog.close()
            }
        })
    }
    
    @IBAction func binimgMakeAction(_ sender: Any) {
        guard let p = self.pb else {
            return
        }
        let binimg = QHBinimageMan.pb2binimg(pb: p, ratio: Int(self.rationS.floatValue), minbin: UInt8(self.minTF.floatValue), maxbin: UInt8(self.maxTF.floatValue))
        self.binimgTV.string = binimg
        self.makefont()
    }
    
    
}

